package org.oschina.api.friendlink.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.friendlink.model.Friendlink;
import org.oschina.api.friendlink.service.FriendlinkService;
import org.oschina.api.friendlink.vo.FriendlinkVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/friendlink")
public class FriendlinkController extends BaseController {

	@Autowired
	private FriendlinkService friendlinkService;
	
	/**
	 * 返回
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<FriendlinkVo> findVo()
	{
		return friendlinkService.findVo();
	}
	
	/**
	 * 创建
	 * @param friendlink
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="text",filter={TextOnlyFilter.class}),
			@ValidateField(name="url",filter={TextOnlyFilter.class}),
			@ValidateField(name="explain",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="color",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="font",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="index",validateValue=true,min=1)
		}) Friendlink friendlink
	)
	{
		return friendlinkService.create(friendlink);
	}
	
	/**
	 * 更新
	 * @param friendlink
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="text",filter={TextOnlyFilter.class}),
			@ValidateField(name="url",filter={TextOnlyFilter.class}),
			@ValidateField(name="explain",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="color",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="font",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="index",validateValue=true,min=1)
		}) Friendlink friendlink
	)
	{
		return friendlinkService.update(friendlink);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}")
	public Result remove
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return friendlinkService.remove(id);
	}
}